package com.easy.work.function.mall.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.easy.work.R;
import com.easy.work.function.mall.bean.ExchangeRecordBean;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author zhangfeng
 * @date 2017/12/19
 * @desc 兑换记录适配器
 */

public class ExchangeRecordAdapter extends ArrayAdapter<ExchangeRecordBean.ListEntity> {

    private List<ExchangeRecordBean.ListEntity> mList;
    private LayoutInflater mInflater;
    private Context context;

    public ExchangeRecordAdapter(Context context, int resource, List<ExchangeRecordBean.ListEntity> objects) {
        super(context, resource, objects);
        mList = objects;
        mInflater = LayoutInflater.from(context);
        this.context = context;
    }

    /**
     * 刷新数据
     */
    public void refreshData(List<ExchangeRecordBean.ListEntity> list) {
        mList.clear();
        addMoreData(list);
    }

    /**
     * 添加更多数据
     */
    public void addMoreData(List<ExchangeRecordBean.ListEntity> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_exchange_record, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ExchangeRecordBean.ListEntity entity = mList.get(position);
        holder.tvContent.setText(TextUtils.isEmpty(entity.goodsName) ? "" : entity.goodsName);
        holder.tvState.setText(entity.status == 1 ? "订单已提交" : "订单已发货");
        Picasso.with(context).load(entity.goodsImageX).into(holder.ivGoods);
        return convertView;
    }

    class ViewHolder {
        @BindView(R.id.iv_goods)
        ImageView ivGoods;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.tv_state)
        TextView tvState;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
